import {
  IGetDecisionFlowSystemVarsRes,
  IGetDictCalcTypeRes,
  getDecisionFlowSystemVars,
  getDictAggFunc,
  getDictApiTemplate,
  getDictCalcType,
  getDictConj,
  getDictExecStatus,
  getDictFieldScope,
  getDictFieldType,
  getDictNodeType,
  getDictObjectType,
  getDictOperator,
  getDictSerializeType,
  getDictListOperator,
  getFeatureExprFuncs
} from '@/api';
import { defineStore } from 'pinia';

type TCommonDict = IGetDictCalcTypeRes;

const dirtToOptions = (dirt: TCommonDict) => {
  return dirt.map((item) => {
    return {
      label: item.dictName!,
      value: item.dictCode! as unknown as string,
      disabled: item.enable !== true
    };
  });
};

// 字典表数据缓存
export const useDictStore = defineStore('dict', {
  state() {
    return {
      /** 字段类型字典 */
      fieldType: [] as TCommonDict,
      fieldTypeMap: {} as Record<string, string>,
      /** 操作符字典 */
      dictOperator: [] as TCommonDict,
      /** 集合操作符字典 */
      dictListOperator: [] as TCommonDict,
      /** 计算类型字典 */
      dictCalcType: [] as TCommonDict,
      /** 连接类型字典 */
      dictConj: [] as TCommonDict,
      /** 字段作用域字典 */
      dictFieldScope: [] as TCommonDict,
      dictFieldScopeMap: {} as Record<string, string>,
      /** 对象类型字典 */
      dictObjectType: [] as TCommonDict,
      /** 序列号类型字典 */
      dictSerializeType: [] as TCommonDict,
      /** 节点类型字典 */
      dictNodeType: [] as TCommonDict,
      /** 聚合函数字典 */
      dictAggFunc: [] as TCommonDict,
      /** 表达式函数字典 */
      dictExpFunc: [] as string[],
      /** 获取API解析模板字典 */
      dictApiTemplate: [] as TCommonDict,
      systemVars: [] as IGetDecisionFlowSystemVarsRes,
      /** 决策流执行状态字典 */
      dictExecStatus: [] as TCommonDict,
      dictExecStatusMap: {} as Record<string, string>
    };
  },
  getters: {
    /** 字段类型字典下拉选项 */
    fieldTypeOptions({ fieldType }) {
      return dirtToOptions(fieldType);
    },
    /** 操作符字典下拉选项 */
    dictOperatorOptions({ dictOperator }) {
      return dirtToOptions(dictOperator);
    },
    /** 计算类型字典下拉选项 */
    dictCalcTypeOptions({ dictCalcType }) {
      return dirtToOptions(dictCalcType);
    },
    /** 连接类型字典下拉选项 */
    dictConjOptions({ dictConj }) {
      return dirtToOptions(dictConj);
    },
    /** 字段作用域字典下拉选项 */
    dictFieldScopeOptions({ dictFieldScope }) {
      return dirtToOptions(dictFieldScope);
    },
    /** 对象类型字典下拉选项 */
    dictObjectTypeOptions({ dictObjectType }) {
      return dirtToOptions(dictObjectType);
    },
    /** 序列号类型字典下拉选项 */
    dictSerializeTypeOptions({ dictSerializeType }) {
      return dirtToOptions(dictSerializeType);
    },
    /** 节点类型字典下拉选项 */
    dictNodeTypeOptions({ dictNodeType }) {
      return dirtToOptions(dictNodeType);
    },
    /** 聚合函数字典下拉选项 */
    dictAggFuncOptions({ dictAggFunc }) {
      return dirtToOptions(dictAggFunc);
    },
    /** API解析模板字典下拉选项 */
    dictApiTemplateOptions({ dictApiTemplate }) {
      return dirtToOptions(dictApiTemplate);
    },
    /** 系统内置变量字典下拉选项 */
    systemVarsOptions({ systemVars }) {
      return systemVars.map((item) => {
        return {
          label: item.varLabel!,
          value: item.varName!,
          varType: item.varType!,
          options: item.options || []
        };
      });
    },
    /** 决策流执行状态字典下拉选项 */
    dictExecStatusOptions({ dictExecStatus }) {
      return dirtToOptions(dictExecStatus);
    }
  },
  actions: {
    /** 字段类型字典 */
    getDictFieldType() {
      if (this.fieldType.length) return Promise.resolve(this.fieldType);
      getDictFieldType().then((res) => {
        this.fieldType = res;
        this.fieldTypeMap = res.reduce((obj, item) => {
          obj[item.dictCode as unknown as string] = item.dictName!;
          return obj;
        }, {} as Record<string, string>);
      });
    },
    /** 操作符字典 */
    getDictOperator() {
      if (this.dictOperator.length) return Promise.resolve(this.dictOperator);
      return getDictOperator().then((res) => {
        this.dictOperator = res;
      });
    },
    /** 集合操作符字典 */
    getDictListOperator() {
      if (this.dictListOperator.length) return Promise.resolve(this.dictListOperator);
      return getDictListOperator().then((res) => {
        this.dictListOperator = res;
      });
    },
    /** 计算类型字典 */
    getDictCalcType() {
      if (this.dictCalcType.length) return Promise.resolve(this.dictCalcType);
      return getDictCalcType().then((res) => {
        this.dictCalcType = res;
      });
    },
    /** 连接类型字典 */
    getDictConj() {
      if (this.dictConj.length) return Promise.resolve(this.dictConj);
      return getDictConj().then((res) => {
        this.dictConj = res;
      });
    },
    /** 字段作用域字典 */
    getDictFieldScope() {
      if (this.dictFieldScope.length) return Promise.resolve(this.dictFieldScope);
      return getDictFieldScope().then((res) => {
        this.dictFieldScope = res;
        this.dictFieldScopeMap = res.reduce((obj, item) => {
          obj[item.dictCode as unknown as string] = item.dictName!;
          return obj;
        }, {} as Record<string, string>);
      });
    },
    /** 获取对象类型字典 */
    getDictObjectType() {
      if (this.dictObjectType.length) return Promise.resolve(this.dictObjectType);
      return getDictObjectType().then((res) => {
        this.dictObjectType = res;
      });
    },
    /** 获取序列号类型字典 */
    getDictSerializeType() {
      if (this.dictSerializeType.length) return Promise.resolve(this.dictSerializeType);
      return getDictSerializeType().then((res) => {
        this.dictSerializeType = res;
      });
    },
    /** 节点类型字典 */
    getDictNodeType() {
      if (this.getDictNodeType.length) return Promise.resolve(this.getDictNodeType);
      return getDictNodeType().then((res) => {
        this.dictNodeType = res as any;
      });
    },
    /** 聚合函数字典 */
    getDictAggFunc() {
      if (this.dictAggFunc.length) return Promise.resolve(this.dictAggFunc);
      return getDictAggFunc().then((res) => {
        this.dictAggFunc = res;
      });
    },
    /** 表达式函数字典 */
    getDictExpFunc() {
      if (this.dictExpFunc.length) return Promise.resolve(this.dictExpFunc);
      return getFeatureExprFuncs().then((res) => {
        this.dictExpFunc = res;
      });
    },
    /** API解析模板字典 */
    getDictApiTemplate() {
      if (this.dictApiTemplate.length) return Promise.resolve(this.dictApiTemplate);
      return getDictApiTemplate().then((res) => {
        this.dictApiTemplate = res;
      });
    },
    /** 系统内置变量字典 */
    getSystemVars() {
      return getDecisionFlowSystemVars().then((res) => {
        this.systemVars = res;
      });
    },
    /** 决策流执行状态字典 */
    getDictExecStatus() {
      if (this.dictExecStatus.length) return Promise.resolve(this.dictExecStatus);
      return getDictExecStatus().then((res) => {
        this.dictExecStatus = res;
        this.dictExecStatusMap = res.reduce((obj, item) => {
          obj[item.dictCode as unknown as string] = item.dictName!;
          return obj;
        }, {} as Record<string, string>);
      });
    },
    fetchDictData() {
      this.getDictFieldType();
      this.getDictOperator();
      this.getDictListOperator();
      this.getDictCalcType();
      this.getDictConj();
      this.getDictFieldScope();
      this.getDictObjectType();
      this.getDictSerializeType();
      this.getDictNodeType();
      this.getDictAggFunc();
      this.getDictExpFunc();
      this.getDictApiTemplate();
      this.getSystemVars();
      this.getDictExecStatus();
    }
  }
});
